package com.xyht.sca_s.student_manage_system.modules.meeting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.meeting.entity.SmsMeetingBuilding;
import com.xyht.sca_s.student_manage_system.modules.meeting.entity.SmsMeetingBuildingFloor;
import com.xyht.sca_s.student_manage_system.modules.meeting.entity.req.SmsMeetingBuildingReq;
import com.xyht.sca_s.student_manage_system.modules.meeting.entity.resp.MeetingBuildingFloorResp;
import com.xyht.sca_s.student_manage_system.modules.meeting.entity.resp.MeetingBuildingResp;
import com.xyht.sca_s.student_manage_system.modules.meeting.entity.resp.OrgMeetingBuildingResp;
import com.xyht.sca_s.student_manage_system.modules.meeting.mapper.SmsMeetingBuildingFloorMapper;
import com.xyht.sca_s.student_manage_system.modules.meeting.mapper.SmsMeetingBuildingMapper;
import com.xyht.sca_s.student_manage_system.modules.meeting.service.SmsMeetingBuildingService;
import com.xyht.sca_s.student_manage_system.modules.org.entity.SmsOrgStructure;
import com.xyht.sca_s.student_manage_system.modules.org.mapper.SmsOrgStructureMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.meeting.constant.BuildingOrFloorType.TYPE_OF_BUILDING;
import static com.xyht.sca_s.student_manage_system.modules.meeting.constant.BuildingOrFloorType.TYPE_OF_FLOOR;

/**
 * <p>
 * 楼栋表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-08-03
 */
@Service
public class SmsMeetingBuildingServiceImpl extends ServiceImpl<SmsMeetingBuildingMapper, SmsMeetingBuilding> implements SmsMeetingBuildingService {
    @Resource
    private SmsMeetingBuildingMapper smsMeetingBuildingMapper;
    @Resource
    private SmsMeetingBuildingFloorMapper smsMeetingBuildingFloorMapper;
    @Resource
    private SmsOrgStructureMapper smsOrgStructureMapper;

    @Override
    public ResponseResult addMeetingBuilding(SmsMeetingBuildingReq buildingReq) {
        //校验参数
        if (isNullOrEmpty(buildingReq.getPrevId()) || isNullOrEmpty(buildingReq.getName())
                || (buildingReq.getType() != TYPE_OF_BUILDING && buildingReq.getType() != TYPE_OF_FLOOR)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        if (buildingReq.getType() == TYPE_OF_BUILDING) {
            SmsOrgStructure org = smsOrgStructureMapper.selectById(buildingReq.getPrevId());
            if (isNullOrEmpty(org)) {
                return CommonResult.failed(CommonCodeEnum.ORGANIZATION_NOT_EXIST);
            }

            SmsMeetingBuilding building = new SmsMeetingBuilding();
            building.setOrgId(buildingReq.getPrevId());
            building.setName(buildingReq.getName());
            smsMeetingBuildingMapper.insert(building);
        } else {
            SmsMeetingBuilding building = smsMeetingBuildingMapper.selectById(buildingReq.getPrevId());
            if (isNullOrEmpty(building)) {
                return CommonResult.failed(CommonCodeEnum.ORGANIZATION_NOT_EXIST);
            }

            SmsMeetingBuildingFloor floor = new SmsMeetingBuildingFloor();
            floor.setBuildingId(buildingReq.getPrevId());
            floor.setName(buildingReq.getName());
            smsMeetingBuildingFloorMapper.insert(floor);
        }

        return CommonResult.success();
    }

    @Override
    @Transactional
    public ResponseResult deleteMeetingBuilding(String id, Integer type) {
        if (isNullOrEmpty(id) || (type != TYPE_OF_BUILDING && type != TYPE_OF_FLOOR)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        if (type == TYPE_OF_BUILDING) {
            SmsMeetingBuilding building = smsMeetingBuildingMapper.selectById(id);
            if (isNullOrEmpty(building)) {
                return CommonResult.failed(CommonCodeEnum.BUILDING_NOT_EXIST);
            }

            smsMeetingBuildingFloorMapper.delete(new LambdaQueryWrapper<SmsMeetingBuildingFloor>()
                    .eq(SmsMeetingBuildingFloor::getBuildingId, building.getId()));

            smsMeetingBuildingMapper.deleteById(id);
        } else {
            SmsMeetingBuildingFloor floor = smsMeetingBuildingFloorMapper.selectById(id);
            if (isNullOrEmpty(floor)) {
                return CommonResult.failed(CommonCodeEnum.FLOOR_NOT_EXIST);
            }

            smsMeetingBuildingFloorMapper.deleteById(id);
        }

        return CommonResult.success();
    }

    @Override
    public ResponseResult getMeetingBuildingTree(Integer pageNum, Integer pageSize, String name) {
        ArrayList<OrgMeetingBuildingResp> meetingBuildingTree = new ArrayList<>();

        LambdaQueryWrapper<SmsOrgStructure> orgQueryWrapper = new LambdaQueryWrapper<SmsOrgStructure>().eq(SmsOrgStructure::getLevel, 1);
        List<SmsOrgStructure> orgList = smsOrgStructureMapper.selectList(orgQueryWrapper);

        for (SmsOrgStructure org : orgList) {
            OrgMeetingBuildingResp orgMeetingBuildingResp = new OrgMeetingBuildingResp();
            orgMeetingBuildingResp.setOrgId(String.valueOf(org.getId()));
            orgMeetingBuildingResp.setOrgName(org.getOrgName());

            ArrayList<MeetingBuildingResp> buildingRespList = new ArrayList<>();
            LambdaQueryWrapper<SmsMeetingBuilding> buildingQueryWrapper = new LambdaQueryWrapper<SmsMeetingBuilding>().eq(SmsMeetingBuilding::getOrgId, org.getId());
            if (!isNullOrEmpty(name)) {
                buildingQueryWrapper.like(SmsMeetingBuilding::getName, name);
            }
            List<SmsMeetingBuilding> buildingList = smsMeetingBuildingMapper.selectList(buildingQueryWrapper);
            for (SmsMeetingBuilding building : buildingList) {
                MeetingBuildingResp buildingResp = new MeetingBuildingResp();
                buildingResp.setId(building.getId());
                buildingResp.setName(building.getName());

                ArrayList<MeetingBuildingFloorResp> floorRespList = new ArrayList<>();
                LambdaQueryWrapper<SmsMeetingBuildingFloor> floorQueryWrapper = new LambdaQueryWrapper<SmsMeetingBuildingFloor>().eq(SmsMeetingBuildingFloor::getBuildingId, building.getId());
                List<SmsMeetingBuildingFloor> floorList = smsMeetingBuildingFloorMapper.selectList(floorQueryWrapper);
                for (SmsMeetingBuildingFloor floor : floorList) {
                    MeetingBuildingFloorResp floorResp = new MeetingBuildingFloorResp();
                    floorResp.setFloorId(floor.getId());
                    floorResp.setFloorName(floor.getName());
                    floorRespList.add(floorResp);
                }

                buildingResp.setFloorRespList(floorRespList);
                buildingRespList.add(buildingResp);
            }

            orgMeetingBuildingResp.setBuildingRespList(buildingRespList);
            meetingBuildingTree.add(orgMeetingBuildingResp);
        }

        return CommonResult.success(meetingBuildingTree);
    }

}
